package com.robot.merchant.controller;


import com.alibaba.fastjson.JSON;
import com.robot.merchant.pojo.Industry;
import com.robot.merchant.service.IndustryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author wuyunbin
 */
@RestController
@CrossOrigin
@RequestMapping("/industry")
public class IndustryJsonController {
    @Autowired
    private IndustryService industryService;

    /**
     * 商家行业列表
     *
     * @return
     */
    @GetMapping(value = "third/findIndustries", produces = "application/json;charset=utf-8")
    public String thirdFindIndustries() {
        return JSON.toJSONString(industryService.findAll());
    }

    /**
     * 根据id查找行业
     *
     * @param id
     * @return
     */
    @GetMapping(value = "third/findIndustryById/{id}", produces = "application/json;charset=utf-8")
    public String thirdFindIndustryById(@PathVariable String id) {
        return JSON.toJSONString(industryService.findById(id));
    }

    /**
     * 根据行业名称查找行业
     *
     * @param industry
     * @return
     */
    @RequestMapping(value = "third/findIndustryByName", method = RequestMethod.POST, produces = "application/json;charset=utf-8")
    public String thirdFindIndustryByName(@RequestBody String industry) {
        Industry industryTemp = JSON.parseObject(industry, Industry.class);
        return JSON.toJSONString(industryService.findByName(industryTemp.getName()));
    }

    /**
     * 增加行业
     *
     * @param industry
     */
    @RequestMapping(value = "third/addIndustry", method = RequestMethod.POST, produces = "application/json;charset=utf-8")
    public void thirdAddIndustry(@RequestBody String industry) {
        industryService.add(JSON.parseObject(industry, Industry.class));
    }

    /**
     * 修改行业
     *
     * @param industry
     */
    @RequestMapping(value = "third/updateIndustry", method = RequestMethod.PUT, produces = "application/json;charset=utf-8")
    public void thirdUpdateIndustry(@RequestBody String industry) {
        industryService.update(JSON.parseObject(industry, Industry.class));
    }

    /**
     * 删除行业
     *
     * @param id
     */
    @RequestMapping(value = "third/deleteIndustryById/{id}", method = RequestMethod.DELETE, produces = "application/json;charset=utf-8")
    public void thirdDeleteIndustryById(@PathVariable String id) {
        industryService.deleteById(id);
    }
}
